const {
    SketchpadData
} = require('../../sketchpad/js/sketchpad-data');

const {
    MusicScoreData
} = require('../../music-score/js/music-score-data');

const {
    History
} = require('./history');

const {
    EditorElement
} = require('./editor-element');

/**
 * 画板的数据
 */
class EditorData extends EditorElement {

    static className() {
        return 'EditorData';
    }

    /**
     * 构造方法
     * @param sketchpadData 画板元素数组
     * @param musicScoreData 谱子数据
     * @param thumbnailBase64Data 缩略图base64
     */
    constructor(sketchpadData, musicScoreData, thumbnailBase64Data) {
        super();
        this._backgroundColor = 'transparent';
        this._sketchpadData = sketchpadData;
        this._thumbnailBase64Data = thumbnailBase64Data;
        this._musicScoreData = musicScoreData;
        this._currentId = (musicScoreData && musicScoreData.currentId) ? musicScoreData.currentId : -1;
        musicScoreData && delete musicScoreData.currentId;
    }

    /**
     * 通过json对象实例化 v1
     * @param JSONObject  目标json对象
     * @returns {*}
     */
    static initWithJSONObject_v1(JSONObject) {
        if (!JSONObject) return null;
        let musicScoreData =  MusicScoreData.initWithJSONObject_v1(JSONObject);
        return new this(new SketchpadData(), musicScoreData, null);
    }

    /**
     * 通过json对象实例化
     * @param JSONObject  目标json对象
     * @returns {*}
     */
    static initWithJSONObject(JSONObject) {
        if (!JSONObject) return null;
        let sketchpadData = SketchpadData.objectFromJSONObject(JSONObject._sketchpadData);
        let musicScoreData = MusicScoreData.objectFromJSONObject(JSONObject._musicScoreData, JSONObject._musicScoreData, MusicScoreData.removeUnderlineCallback());
        return new this(sketchpadData, musicScoreData, JSONObject._thumbnailBase64Data, JSONObject);
    }

    get currentId() {
        return this._currentId;
    }

    set currentId(value) {
        this._currentId = value;
    }

    /**
     * 谱子数据
     * @returns {*}
     */
    get musicScoreData() {
        if (!this._musicScoreData) {
            this._musicScoreData = new MusicScoreData();
        }
        return this._musicScoreData;
    }

    set musicScoreData(value) {
        this._musicScoreData = value;
    }

    /**
     * 画板数据
     * @returns {*}
     */
    get sketchpadData() {
        if (!this._sketchpadData) {
            this._sketchpadData = new SketchpadData();
        }
        return this._sketchpadData;
    }

    set sketchpadData(value) {
        this._sketchpadData = value;
    }

    get backgroundColor() {
        return this._backgroundColor;
    }

    set backgroundColor(value) {
        this._backgroundColor = value;
    }

    get thumbnailBase64Data() {
        return this._thumbnailBase64Data;
    }

    set thumbnailBase64Data(value) {
        this._thumbnailBase64Data = value;
    }

    get downloadBase64Data() {
        return this._downloadBase64Data;
    }

    set downloadBase64Data(value) {
        this._downloadBase64Data = value;
    }

    /**
     * 历史记录
     * @returns {*}
     */
    get history() {
        if (!this._history) {
            this._history = new History();
        }
        return this._history;
    }

    set history(value) {
        this._history = value;
    }

    /**
     * 打谱、歌唱等模块中点击"播放"、"编辑"时的临时存储的来源模块的信息
     * @returns {*}
     */
    get from() {
        return this._from;
    }
    set from(value) {
        this._from = value;
    }

    /**
     * 打谱、歌唱等模块中点击"播放"、"编辑"时的临时存储的唯一id(当前时间戳)，用来防止来回跳转时添加多个缩略图
     * @returns {*}
     */
    get tmpId() {
        return this._tmpId;
    }

    set tmpId(value) {
        this._tmpId = value;
    }
}

module.exports = {
    EditorData
};
